﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FAMailUtils.MailGun
{
    public class Domain
    {
        public string created_at { get; set; }
        public string smtp_login { get; set; }
        public string name { get; set; }
        public string smtp_password { get; set; }
        public string wildcard { get; set; }
        public string spam_action { get; set; }
        public DomainItem detail { get; set; }
        public int receiving_dns_record { get { return detail == null ? 0 : detail.receiving_dns_records.Count; } }
        public int sending_dns_record { get { return detail == null ? 0 : detail.sending_dns_records.Count; } }
    }
    public class DomainList
    {
        public bool isGetOK { get; set; }
        public string message { get; set; }
        public decimal total_count { get; set; }
        public List<Domain> items { get; set; }
    }
  

    public class receiving_dns_record
    {
        public string priority { get; set; }
        public string record_type { get; set; }
        public string valid { get; set; }
        public string value { get; set; }
    }
    public class  sending_dns_record
    {
        public string name { get; set; }
        public string record_type { get; set; }
        public string valid { get; set; }
        public string value { get; set; }
    }
    public class DomainItem
    {
        public Domain domain { get; set; }
        public List<receiving_dns_record> receiving_dns_records { get; set; }
        public List<sending_dns_record> sending_dns_records { get; set; }
        public bool isGetOK { get; set; }
        public string message { get; set; }
    }

    public class DomainUtils
    {
        string ApiKey;
        public DomainUtils(string apiKey)
        {
            ApiKey = apiKey;
        }
        public DomainList GetDomains()
        {
            DomainList result = new DomainList();
            try
            {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.mailgun.net/v2";
                client.Authenticator =
                        new HttpBasicAuthenticator("api",
                                                   ApiKey);
                RestRequest request = new RestRequest();
                request.Resource = "domains";
                request.AddParameter("skip", 0);
                //request.AddParameter("limit", 3);
                IRestResponse response = client.Execute(request);


                result = Newtonsoft.Json.JsonConvert.DeserializeObject<DomainList>(response.Content);
                result.isGetOK = true;
                return result;
            }
            catch (Exception ex)
            {
                result.isGetOK = false; result.message = ex.Message;
            }
            return result;
        }
        public DomainItem GetDomain(string domainName)
        {
            DomainItem result = new DomainItem();
            try
            {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.mailgun.net/v2";
                client.Authenticator =
                        new HttpBasicAuthenticator("api",
                                                   ApiKey);
                RestRequest request = new RestRequest();
                request.AddParameter("domain",
                                    domainName, ParameterType.UrlSegment);
                request.Resource = "/domains/{domain}";
                IRestResponse response = client.Execute(request);
                result = Newtonsoft.Json.JsonConvert.DeserializeObject<DomainItem>(response.Content);
                result.isGetOK = true;
                return result;
            }
            catch (Exception ex)
            {
                result.isGetOK = false; result.message = ex.Message;
            }
            return result;
        }
        public DomainList GetDomains(bool getStatus)
        {
            DomainList result = GetDomains();
            if (result.isGetOK && getStatus)
            {
                foreach (var item in result.items)
                {
                    DomainItem domain = GetDomain(item.name);
                    if (domain.isGetOK)
                        item.detail = domain;
                }
            }
            return result;
        }
        public bool AddDomain(string domainName, string smtp_password)
        {
            try
            {
                if (domainName.ToLower() == "gmail.com") domainName = "smtp.gmail.com";
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.mailgun.net/v2/";
                client.Authenticator =
                        new HttpBasicAuthenticator("api",
                                                   ApiKey);
                RestRequest request = new RestRequest();
                request.Resource = "domains";
                request.AddParameter("name", domainName);
                request.AddParameter("smtp_password", smtp_password);
                request.AddParameter("wildcard", true);
                request.Method = Method.POST;
                IRestResponse respoint = client.Execute(request);
                return true;
            }
            catch (Exception ex) { }
            return false;
        }
        public bool DeleteDomain(string domainName)
        {
            try
            {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.mailgun.net/v2";
                client.Authenticator =
                        new HttpBasicAuthenticator("api",
                                                   ApiKey);
                RestRequest request = new RestRequest();
                request.Resource = "/domains/{name}";
                request.AddUrlSegment("name", domainName);
                request.Method = Method.DELETE;
                IRestResponse respoint = client.Execute(request);
                return true;
            }
            catch (Exception ex) { }
            return false;
        }

    }
}
